package ru.mcfr.oxygen.framework.operations;

import ro.sync.ecss.extensions.api.*;
import ro.sync.ecss.extensions.api.node.AuthorDocumentFragment;

public class InsertInlineSublinesOperation implements AuthorOperation {


    public void doOperation(AuthorAccess authorAccess, ArgumentsMap args) throws IllegalArgumentException, AuthorOperationException {

        AuthorDocumentController controller = authorAccess.getDocumentController();
        String selectedText = authorAccess.getEditorAccess().getSelectedText();

        AuthorDocumentFragment nf = null;

        int where = authorAccess.getEditorAccess().getSelectionStart();
        try {
            if (!selectedText.isEmpty()) {
                controller.delete(authorAccess.getEditorAccess().getSelectionStart(), authorAccess.getEditorAccess().getSelectionEnd() - 1);
                nf = controller.createNewDocumentFragmentInContext("<" + args.getArgumentValue("тип индекса") + " id=\"\">" + selectedText + "</" + args.getArgumentValue("тип индекса") + ">", where);
            } else
                nf = controller.createNewDocumentFragmentInContext("<" + args.getArgumentValue("тип индекса") + " id=\"\"/>", authorAccess.getEditorAccess().getCaretOffset());
        } catch (Exception ex) {
            ex.printStackTrace();
            authorAccess.getWorkspaceAccess().showInformationMessage(ex.getMessage());
        }

        if (nf != null)
            authorAccess.getDocumentController().insertFragment(where, nf);

    }


    public ArgumentDescriptor[] getArguments() {
        ArgumentDescriptor[] res = {
                new ArgumentDescriptor("тип индекса", ArgumentDescriptor.TYPE_STRING, "верхний_индекс/нижний_индекс")
        };
        return res;
    }


    public String getDescription() {
        // TODO Auto-generated method stub
        return "Верхние нижние индексы";
    }

}
